//
//  YH_ColumnCatViewController.m
//  YinHang
//
//  Created by 张亚哲 on 14/10/25.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "YH_ColumnCatViewController.h"

//切换视图
#import "Colum_NavViewController.h"
#import "Colum_IndexViewController.h"
#import "Colum_TitleViewController.h"

//修改功能
#import "Fun_AlertViewController.h"

@interface YH_ColumnCatViewController ()<fun_alertDelegate>{
    
    __block NSInteger selectedIndex;//选中列
    
    __weak IBOutlet UIView *ChangeView;
    // 加载选项卡
    HMSegmentedControl *segmentedControl;
    UIButton *rightBtn;
    
    Colum_NavViewController *C_NVC;
}

@end

@implementation YH_ColumnCatViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //标题
    self.navigationItem.title = @"栏目管理";
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = - 10;
    if (ios7) {
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftBarButtonItem];
    }else{
        self.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
    }
    rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    //[rightBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [rightBtn setTitle:@"修改" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(alert:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    if (ios7) {
        self.navigationItem.rightBarButtonItems = @[negativeSpacer,rightBarItem];
    }else{
        self.navigationItem.rightBarButtonItems = @[rightBarItem];
    }

    
    [self setUI];
    [self setSeg];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark CustomMethod

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setUI{
    UIScreen *screen = [UIScreen mainScreen];
    ChangeView.frame = CGRectMake(0, 35, screen.bounds.size.width, screen.bounds.size.height - 35);
    //加载手势
    //加载手势(向左轻扫)
    UISwipeGestureRecognizer *leftSgr = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    leftSgr.direction = UISwipeGestureRecognizerDirectionLeft;
    [ChangeView addGestureRecognizer:leftSgr];
    //加载手势(向右轻扫)
    UISwipeGestureRecognizer *rightSgr = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    rightSgr.direction = UISwipeGestureRecognizerDirectionRight;
    [ChangeView addGestureRecognizer:rightSgr];

    
}
-(void)setSeg{
    // 加载选项卡
    segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"导航栏目", @"首页栏目", @"网站标题"]];
    selectedIndex = 0;
    C_NVC = [[Colum_NavViewController alloc]initWithNibName:@"Colum_NavViewController" bundle:nil];
    Colum_IndexViewController *C_IVC = [[Colum_IndexViewController alloc]initWithNibName:@"Colum_IndexViewController" bundle:nil];
    Colum_TitleViewController *C_TVC = [[Colum_TitleViewController alloc]initWithNibName:@"Colum_TitleViewController" bundle:nil];
    
    [ChangeView addSubview:C_TVC.view];
    [ChangeView addSubview:C_IVC.view];
    [ChangeView addSubview:C_NVC.view];
    
    [segmentedControl setIndexChangeBlock:^(NSUInteger index) {
        
        selectedIndex = index;
        switch (selectedIndex) {
            case 0:{
                [UIView animateWithDuration:2 animations:^{
                     [ChangeView bringSubviewToFront:C_NVC.view];
                }];
                [C_NVC reFreshData];
                rightBtn.hidden = NO;
            }break;
            case 1:{
                [UIView animateWithDuration:2 animations:^{
                    [ChangeView bringSubviewToFront:C_IVC.view];
                }];
                [C_IVC reFreshData];
                rightBtn.hidden = YES;
            }break;
            case 2:{
                [UIView animateWithDuration:2 animations:^{
                    [ChangeView bringSubviewToFront:C_TVC.view];
                }];
                [C_TVC reFreshData];
                rightBtn.hidden = YES;
            }break;
            default:
                break;
        }
        
    }];
    [segmentedControl setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 35)];
    [self.view addSubview:segmentedControl];
}

- (void)swipe:(UISwipeGestureRecognizer *)sgr {
    
    NSString *directionStr = @"轻扫手势";
    
    switch (sgr.direction) {
        case UISwipeGestureRecognizerDirectionLeft:{
            directionStr = @"向左轻扫";
                if (selectedIndex == 2) {
                
                }else{
                    selectedIndex++;
                    [segmentedControl setSelectedIndex:selectedIndex animated:YES];
                }
            }break;
        case UISwipeGestureRecognizerDirectionRight:{
            directionStr = @"向右轻扫";
                if (selectedIndex == 0) {
                
                }else{
                    selectedIndex--;
                    [segmentedControl setSelectedIndex:selectedIndex animated:YES];
                }
            }break;
        default:
            break;
    }
    
}

-(void)dealloc{
    NSLog(@"栏目管理dealloc");
    C_NVC = nil;
}

-(void)alert:(id)sender{
    Fun_AlertViewController *alertView = [[Fun_AlertViewController alloc]initWithId:C_NVC.idstr titleArr:C_NVC.titleArr titleDic:C_NVC.titleDic];
    alertView.delegate = self;
    [self.navigationController pushViewController:alertView animated:YES];
}

#pragma mark Delegate
-(void)refreshAlertData{
    [C_NVC reFreshData];
}

@end
